package com.controller;

import com.pojo.ApplyChild;
import com.pojo.ApplyMaster;
import com.pojo.User;
import com.service.ApplyChildService;
import com.service.ApplyMasterService;
import com.util.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("apply/handApply")
public class ApplyMasterHandler {
    @Resource
    private ApplyMasterService applyMasterService;

    @RequestMapping("/searchByApplyMasterPager")
    @ResponseBody
    public Pager<ApplyMaster> searchByApplyMasterPager(ApplyMaster applyMaster,Integer limit,Integer offset){
        Pager<ApplyMaster> pager=new Pager<>();
        pager.setLimit(limit);
        pager.setOffSet(offset);
        pager.setCondition(applyMaster);
        return applyMasterService.searchByPagerApplyMaster(pager);
    }

    @RequestMapping("/addApplyMaster")
    @ResponseBody
    public ApplyMaster addApplyMaster(HttpSession session){
        User user = (User) session.getAttribute("loginUser");
        ApplyMaster applyMaster=new ApplyMaster();
        applyMaster.setDepartmentCode(user.getDepartmentCode());
        Integer s=(int) (Math.random()*(9999-1000+1))+1000;
        SimpleDateFormat sm=new SimpleDateFormat("yyyyMMdd");
        String no = sm.format(new Date())+s.toString();
        applyMaster.setApplyMasterNo(no);
        int i =applyMasterService.add(applyMaster);
        if(i>0){
            applyMaster=applyMasterService.getLast();
        }

        return applyMaster;
    }

    @RequestMapping("/TiJiao")
    @ResponseBody
    public String TiJiao(String applyMasterNo){
        String msg="提交失败！";
        int i =applyMasterService.TiJiao(applyMasterNo);
        if(i>0){
            msg="提交成功！";
        }
        return msg;
    }



    @Resource
    private ApplyChildService applyChildService;
    @RequestMapping("/searchByApplyChild")
    @ResponseBody
    public Pager<ApplyChild> searchByApplyChild(String applyMasterNo){
        Pager<ApplyChild> pager=new Pager<>();
        return applyChildService.searchApplyChild(pager,applyMasterNo);
    }


    @RequestMapping("/to")
    public String to(String applyMasterId, Model model){
        List<ApplyChild> applyChildList =applyChildService.searchByApplyChild(applyMasterId);
        model.addAttribute("applyChildList",applyChildList);
        return "apply/applyChild";
    }

    @RequestMapping("/to2")
    public String to2(String applyMasterId,String applyMasterNo, Model model){
        List<ApplyChild> applyChildList2 =applyChildService.searchByApplyChild(applyMasterId);
        model.addAttribute("applyChildList2",applyChildList2);
        model.addAttribute("applyMasterNo",applyMasterNo);
        return "apply/upHandApply";
    }

    @RequestMapping("/delApplyChild")
    @ResponseBody
    public String delApplyChild(String id){
        String msg="删除失败！";
        int i =applyChildService.del(id);
        if(i>0){
            msg="删除成功！";
        }
        return msg;
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(ApplyChild applyChild){
        String msg="添加失败！";
        int i =applyChildService.add(applyChild);
        if(i>0){
            msg="添加成功！";
        }
        return msg;
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(ApplyChild applyChild){
        String msg="修改失败！";
        int i =applyChildService.update(applyChild);
        if(i>0){
            msg="修改成功！";
        }
        return msg;
    }

}
